package com.example.blibli;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentActivity;
import androidx.viewpager2.adapter.FragmentStateAdapter;
import androidx.viewpager2.widget.ViewPager2;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.MenuItem;

import com.example.blibli.homescreen.add.AddFragment;
import com.example.blibli.homescreen.dynamicstate.DynamicStateFragment;
import com.example.blibli.homescreen.homepage.HomePageFragment;
import com.example.blibli.homescreen.my.MyFragment;
import com.example.blibli.homescreen.vipchase.VipChaseFragment;
import com.google.android.material.bottomnavigation.BottomNavigationView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends AppCompatActivity {
    private ViewPager2 mHomePageViewPager;
    private BottomNavigationView mHomePageNavigationView;
    private List<Fragment> mHomePageFragments; // 存储fragments
    MenuItem mMenuItem;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        initViews();
        TabsAdapter adapter = new TabsAdapter(this, mHomePageFragments);
        mHomePageViewPager.setAdapter(adapter);

        mHomePageNavigationView.setOnNavigationItemSelectedListener(item -> {
            switch (item.getItemId()) {
                case R.id.home:
                    mHomePageViewPager.setCurrentItem(0);
                    break;
                case R.id.dynamic_state:
                    mHomePageViewPager.setCurrentItem(1);
                    break;
                case R.id.add:
                    mHomePageViewPager.setCurrentItem(2);
                    break;
                case R.id.vip_chase:
                    mHomePageViewPager.setCurrentItem(3);
                    break;
                case R.id.my:
                    mHomePageViewPager.setCurrentItem(4);
                    break;
                default:
                    break;
            }
            return true;
        });
        mHomePageViewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
                super.onPageScrolled(position, positionOffset, positionOffsetPixels);
            }

            @Override
            public void onPageSelected(int position) {
                if (mMenuItem == null) {
                    mMenuItem = mHomePageNavigationView.getMenu().getItem(0); // 默认打开第0个tab
                }
                mMenuItem.setChecked(false); // 将上次的选择设置为false，等待下一次选择。
                mMenuItem = mHomePageNavigationView.getMenu().getItem(position);
                mMenuItem.setChecked(true);
                super.onPageSelected(position);
            }

            @Override
            public void onPageScrollStateChanged(int state) {
                super.onPageScrollStateChanged(state);
            }
        });
    }

    private void initViews() {
        mHomePageViewPager = findViewById(R.id.homepage_viewpager);
        mHomePageNavigationView = findViewById(R.id.homepage_navigation);
        mHomePageFragments = new ArrayList<>();
        mHomePageFragments.add(new HomePageFragment());
        mHomePageFragments.add(new DynamicStateFragment());
        mHomePageFragments.add(new AddFragment());
        mHomePageFragments.add(new VipChaseFragment());
        mHomePageFragments.add(new MyFragment());
    }

    private class TabsAdapter extends FragmentStateAdapter {
        List<Fragment> fragments;

        public TabsAdapter(FragmentActivity fa, List<Fragment> fragments) {
            super(fa);
            this.fragments = fragments;
        }


        @NonNull
        @Override
        public Fragment createFragment(int position) {
            return fragments.get(position);
        }

        @Override
        public int getItemCount() {
            return fragments.size();
        }
    }
}